import { SchemaManage } from '@/components/SchemaComponent';
import React, { useRef, useMemo, useState } from 'react';
import { connect, history } from 'umi';
import { BidevaluaManageAPI } from '../common/type';
import { ManageTableSwitch } from '../mconfig';
import { ManageServices } from '../services';
import { PlusOutlined } from '@ant-design/icons';
import { getKeyRouter, getTenantID } from '@/utils';
import { RevokeModal } from '../../Acomponents/RevokeModal';
import { ILoadMapTypeAll } from '@/common';
import { CancelModal } from '@/components/CancelTextModal';

const ManageView: React.FC<BidevaluaManageAPI.IMaterialManageView> = ({ curRes, locationProps }) => {
  /**
   * 管理页services
   * */
  const { searchList, reloadRequest, pagination, data, loading, manageReload } = ManageServices(curRes);

  /**
   * 管理页的ref
   * */
  const ManageViewRef: any = useRef();

  /**
   * 撤销弹窗
   * */
  const [invalidState, setInvalidState] = useState<Record<string, any>>({});

  /**
   * 作废弹窗
   * */
  const [isShowCancelModal, setIsShowCancelModal] = useState<Record<string, any>>({
    isShow: false,
    recordObj: {},
  });
  /**
   * 头部权限的切换
   * */
  const switchList: BidevaluaManageAPI.IManageTabsSwitchType = useMemo(() => {
    return [];
  }, [curRes]);

  /**
   * 头部切换方法
   * */
  const onSwitch = (key: string | undefined | number, searchValues: any, params: any) => {
    reloadRequest({ ...params, ...searchValues });
  };

  /**
   * 重置方法
   * */
  const onReset = (reqParams: BidevaluaManageAPI.IAssetTableRequestType) => {
    reloadRequest({ ...reqParams });
  };

  /**
   * 搜索方法
   * */
  const onSearch = (reqParams: BidevaluaManageAPI.IAssetTableRequestType) => {
    reloadRequest({ ...reqParams });
  };

  /**
   * 分页方法
   * */
  const onPageChange = (current: any, pageSize: any, params: any) => {
    reloadRequest({ pageSize, current, ...params });
  };

  /**
   * 左侧按钮
   * */
  const actionList: BidevaluaManageAPI.IActionList = useMemo(() => {
    return [
      {
        title: '添加',
        visible: curRes?.['/purchaseSourceExpertController/addRepresentative'] || false,
        type: 'primary',
        icon: <PlusOutlined />,
        onClick: () => {
          history.push({
            pathname: `/purchaseSourceExpertController/addRepresentative/:${getKeyRouter()}${getKeyRouter()}`,
            state: {
              pageType: 'add',
              record: null,
              returnPathName: locationProps?.pathname,
            },
          });
        },
      },
    ];
  }, [curRes]);

  /**
   * Table的操作方法
   * */
  const getTableOperateFuncs = {
    onView: (record) => {
      history.push({
        pathname: `/purchaseSourceExpertController/getSourceExpertById/{id}/{currentUserID}/:${getKeyRouter()}${getKeyRouter()}`,
        state: {
          pageType: 'detail',
          record,
          returnPathName: locationProps?.pathname,
        },
      });
    },
    onEdit: (record) => {
      history.push({
        pathname: `/purchaseSourceExpertController/editRepresentative/:${getKeyRouter()}${getKeyRouter()}`,
        state: {
          pageType: 'edit',
          record,
          returnPathName: locationProps?.pathname,
        },
      });
    },
    onInvalid: (record) => {
      setInvalidState({
        isShow: true,
        resObj: record,
      });
    },
    onOAPage: (record) => {
      history.push({
        pathname: `/purchaseSourceExpertController/dealHomePage/:${getKeyRouter()}${getKeyRouter()}`,
        state: record,
      });
    },
    onCancel: (record) => {
      setIsShowCancelModal({
        isShow: true,
        recordObj: record,
      });
    },
  };

  /**
   * Table的columns
   * */
  const getTableColumn: Record<string, any>[] = useMemo(() => {
    return ManageTableSwitch(curRes, getTableOperateFuncs);
  }, [curRes]);

  return (
    <>
      <SchemaManage
        ref={ManageViewRef}
        searchProps={{
          switchList,
          searchList,
          onSwitch,
          onReset,
          onSearch,
        }}
        actionList={actionList}
        tableProps={{
          columns: getTableColumn,
          dataSource: data?.rows,
          rowKey: 'id',
          loading: loading,
          pagination: pagination as any,
          onPageChange: onPageChange,
        }}
      />
      {invalidState?.isShow && (
        <RevokeModal
          resObj={invalidState?.resObj}
          revokeRequestUrl={'/purchaseSourceExpertController/invalid'}
          onClose={() => setInvalidState({})}
          reqBody={{ state: `${getTenantID()}${ILoadMapTypeAll?.EXPERT_BUSINESSTYPE}1000` }}
          modalTip={
            <div style={{ color: '#000' }}>
              确认是否撤销【<span style={{ color: 'red' }}>{invalidState?.resObj?.purchProjectName}</span>】的审核,撤销后可重新提交！
            </div>
          }
          reloadRequest={manageReload}
        />
      )}

      {/* 作废弹窗 */}
      {isShowCancelModal?.isShow && (
        <CancelModal
          checkRows={isShowCancelModal?.recordObj}
          cancelTypeDic={ILoadMapTypeAll.EXPERT_CANCEL}
          cancelFormTypeRequired={true}
          cancelFormReasonRequired={true}
          modalTitle={'作废评标代表'}
          cancelText={isShowCancelModal?.recordObj?.fullName || null}
          cancelRequestUrl={'/purchaseSourceExpertController/cancel'}
          reloadManage={manageReload}
          cancelRequestType="PUT"
          cancelRequestBody={{ state: `${getTenantID()}${ILoadMapTypeAll?.EXPERT_BUSINESSTYPE}1003` }}
          onClose={() => {
            setIsShowCancelModal({
              isShow: false,
              signTitle: '',
            });
          }}
        />
      )}
    </>
  );
};

export default connect(({ baseLogin, navTab }: Record<string, any>) => ({
  curRes: baseLogin?.permissions,
  navTab,
}))(ManageView);
